import { getGoodsInfo, addOrUpdateShopCar } from '@/api'
import { getUUID } from '@/utils/uuid_token'
const state = {
  goodsInfo: {},
  uuid_token: getUUID(),
}
const actions = {
  async getGoodsInfo(context, skuid) {
    let result = await getGoodsInfo(skuid)
    if (result.code == 200) {
      context.commit('GETGOODSINFO', result.data)
    }
  },
  async addShopCar(context, { skuId, skuNum }) {
    //async函数执行返回结果一定是一个promise（要么成功，要么失败）
    let result = await addOrUpdateShopCar(skuId, skuNum)
    if (result.code == 200) {
      return 'OK'
    } else {
      //返回的是失败的标记
      return Promise.reject(new Error('faile'))
    }
  },
}
const mutations = {
  GETGOODSINFO(state, data) {
    state.goodsInfo = data
  },
}
const getters = {
  categoryView(state) {
    return state.goodsInfo.categoryView || {}
  },
  skuInfo(state) {
    return state.goodsInfo.skuInfo || {}
  },
  spuSaleAttrList(state) {
    return state.goodsInfo.spuSaleAttrList || []
  },
}
export default {
  namespaced: 'detail',
  state,
  actions,
  mutations,
  getters,
}
